CS 5010: Problem Set 9

Out: Monday, November 10, 2014

Due: Monday, November 17, 2014 at 600pm local time.


The goal of this problem set is to help you design simple class systems and methods on them.

As always, you must follow the design recipe.

Please restrict yourself to the language features discussed in class. In particular, you may NOT use state or inheritance.

You must use #lang racket, rackunit, "extras.rkt", 2htdp/universe, and 2htdp/image. So your file should begin something like

#lang racket
(require rackunit)
(require "extras.rkt")
(require 2htdp/universe)   
(require 2htdp/image)      
You may require "sets.rkt" if you need it. You may not require any other libraries.

You have taken a job in a toy factory. You job is to simulate the following marvelous toy:

Your solution should be a file named toys.rkt should provide the following classes and functions:

World%     -- a class that satisfies the World<%> interface (shown below).
SquareToy% -- a class that satisfies the Toy<%> interface
CircleToy% -- a class that satisfies the Toy<%> interface

make-world : PosInt -> World%
RETURNS: a world with a target, but no toys, and in which any
toys created in the future will travel at the given speed (in pixels/tick).

run : PosNum PosInt -> World%
GIVEN: a frame rate (in seconds/tick) and a square-speed (in pixels/tick),
creates and runs a world.  Returns the final state of the world.

make-square-toy : PosInt PosInt PosInt -> SquareToy%
GIVEN: an x and a y position, and a speed
RETURNS: an object representing a square toy at the given position,
travelling right at the given speed.

make-circle-toy : PosInt PosInt -> CircleToy%
GIVEN: an x and a y position
RETURNS: an object represeenting a circle toy at the given position.

Interfaces:

(define World<%>
  (interface ()

    ;; -> World<%>
    ;; Returns the World<%> that should follow this one after a tick
    on-tick                             

    ;; Integer Integer MouseEvent -> World<%>
    ;; Returns the World<%> that should follow this one after the
    ;; given MouseEvent
    on-mouse

    ;; KeyEvent -> World<%>
    ;; Returns the World<%> that should follow this one after the
    ;; given KeyEvent
    on-key

    ;; -> Scene
    ;; Returns a Scene depicting this world
    ;; on it.
    on-draw 
    
    ;; -> Integer
    ;; RETURN: the x and y coordinates of the target
    target-x
    target-y

    ;; -> Boolean
    ;; Is the target selected?
    target-selected?

    ;; -> ListOfToy<%>
    get-toys

))

(define Toy<%> 
  (interface ()

    ;; -> Toy<%>
    ;; returns the Toy that should follow this one after a tick
    on-tick                             

    ;; Scene -> Scene
    ;; Returns a Scene like the given one, but with this toy drawn
    ;; on it.
    add-to-scene

    ;; -> Int
    toy-x
    toy-y

    ;; -> ColorString
    ;; returns the current color of this toy
    toy-color

    ))

Note 1: we have said that interfaces are for abstracting over multiple classes with common method names. You will likely only have one class that implements World<%>, but this interface gives the testing framework a known set of methods to call.

Note 2: these interfaces list only the methods that we need in order to test your program; your classes are likely to have other methods as well. If your program relies on other methods being in each Toy, you should add those to the Toy<%> interface. These should be clearly marked.

Qualification tests are in ps09-qualification.rkt.

When you do this problem, remember the principle of Iterative Development: get something simple working, and then add features as necessary. This problem has many simple features for you to add. For what it's worth, my solution was 470 lines, of which 173 lines was devoted to the world and the target, 85 to squares, and 63 to circles.


Last modified: Wed Nov 12 21:52:29 Eastern Standard Time 2014